package aiaudio.storage.simple;

import aiaudio.storage.api.IArtist;
import aiaudio.storage.api.IAudioTrainingSet;
import aiaudio.storage.api.IUser;
import aiaudio.storage.api.InternalException;
import aiaudio.storage.api.Tuple;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Nastya
 */
public class AudioTrainingSet implements IAudioTrainingSet {

    private List<IUser> users;
    private List<IArtist> artists;
    private HashMap<IUser, List<Tuple<IArtist, Double>>> ratings;

    public AudioTrainingSet(List<IUser> users, List<IArtist> artists,
            HashMap<IUser, List<Tuple<IArtist, Double>>> ratings) {
        this.users = users;
        this.artists = artists;
        this.ratings = ratings;
    }

    @Override
    public Iterator<IArtist> getArtistsIterator() {
        return artists.iterator();
    }

    @Override
    public Iterator<IUser> getUsersIterator() {
        return users.iterator();
    }

    @Override
    public Iterator<double[]> getTrainingRatings(List<IArtist> artists,
            List<IUser> users) throws InternalException {
        List<double[]> ratingsList = new ArrayList<double[]>();
        for (IUser IUser : users) {
            ratingsList.add(getTrainingRatings(artists, IUser));
        }
        return ratingsList.iterator();
    }

    @Override
    public double[] getTrainingRatings(List<IArtist> artists, IUser user)
            throws InternalException {
        double[] resultRatings = new double[artists.size()];
        List<Tuple<IArtist, Double>> ratingsByUser = ratings.get(user);
        for (Tuple<IArtist, Double> tuple : ratingsByUser) {
            if (artists.contains(tuple.getFirst())) {
                int index = artists.indexOf(tuple.getFirst());
                resultRatings[index] = tuple.getSecond();
            }
        }

        return resultRatings;
    }

    @Override
    public double getTrainingRating(IArtist artists, IUser user)
            throws InternalException {
        List<Tuple<IArtist, Double>> ratingsByUser = ratings.get(user);
        if (ratingsByUser == null) {
            return -1;
        }
        for (Tuple<IArtist, Double> tuple : ratingsByUser) {
            if (artists.equals(tuple.getFirst())) {
                return tuple.getSecond();
            }
        }

        return -1;
    }

    @Override
    public String toString() {
        return "AudioTrainingSet{" + "users=" + users + ",\r\n artists="
                + artists + ",\r\n ratings=" + ratings + '}';
    }
}
